package
{
	
	/**
	 * 静态类，用于转换事件格式的参数
	 * @author 杨伟华
	 * @version 2.0
	 *
	 * Created 1/15/2013 11:31 PM
	 */
	public class TimeFormat
	{
		/**
		 * 将秒格式转换成hh:mm:ss
		 * @param ss0 Number,需要转换的秒数;
		 * @return 返回hh:mm:ss格式的字符串;		 *
		 */
		public static function ss2hhmmss(ss0:Number, isForScorm:Boolean = false):String
		{
			if (ss0 >= 0)
			{
				var ss:Number = ss0 % 60;
				var mm0:Number = (ss0 - ss) / 60;
				var sst:String;
				if (!isForScorm)
				{
					sst = String(Math.floor(ss));
				}
				else
				{
					sst = setZeroPostfix(ss);
				}
				var mm:uint = mm0 % 60;
				var hh:uint = (mm0 - mm) / 60;
				return setZeroPrefix(hh) + ":" + setZeroPrefix(mm) + ":" + setZeroPrefix(sst, "00");
			}
			else
			{
				return null;
			}
		}
		
		/**
		 * 将hh:mm:ss转换成秒格式
		 * @param	hh:mm:ss格式的字符串
		 * @return	秒格式
		 */
		public static function hhmmss2ss(hhmmss:String):Number
		{
			hhmmss = "00:00:" + hhmmss;
			var ssA:Array = hhmmss.split(":");
			var ss:Number = Number(ssA.pop());
			ss += Number(ssA.pop()) * 60;
			ss += Number(ssA.pop()) * 3660;
			return ss;
		}
		
		/**
		 * 整数位前补白,前填补0;
		 * @param	number 需要补白的数字;
		 * @param	prefixFormate 整数位数;
		 * @return 补白后的字符串;
		 */
		public static function setZeroPrefix(number:*, prefixFormate:String = "00"):String
		{
			var nStr:String = String(number);
			var splitByDot:Array = nStr.split(".");
			var offset:int = String(splitByDot[0]).length - prefixFormate.length;
			return (offset >= 0) ? nStr : prefixFormate.slice(offset) + nStr;
		}
		
		/**
		 * 小数位后补白,后填补0;
		 * @param	number 需要补白的数字;
		 * @param	postfixFormate 小数位数;
		 * @return 补白后的字符串;
		 */
		public static function setZeroPostfix(number:*, postfixFormate:String = "00"):String
		{
			var splitByDot:Array = String(number).split(".");
			return splitByDot[0] + "." + (splitByDot[1]||"0" + postfixFormate).slice(0, postfixFormate.length);
		}
	}
}